<template>
  <div class="product-management">
    <!-- 侧边栏 -->
    <div class="sidebar">
      <div class="sidebar-header">
        <div class="logo">
          <i class="icon">📦</i>
          <span>商品管理</span>
        </div>
        <div class="triangle"></div>
      </div>
      
      <nav class="sidebar-nav">
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'products' }"
          @click="navigateTo('products')"
        >
          <i class="nav-icon">🛍️</i>
          <span>商品列表</span>
        </div>
        
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'brands' }"
          @click="navigateTo('brands')"
        >
          <i class="nav-icon">🏷️</i>
          <span>品牌列表</span>
        </div>
        
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'types' }"
          @click="navigateTo('types')"
        >
          <i class="nav-icon">📂</i>
          <span>类型列表</span>
        </div>
        
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'colors' }"
          @click="navigateTo('colors')"
        >
          <i class="nav-icon">🎨</i>
          <span>颜色列表</span>
        </div>
        
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'sizes' }"
          @click="navigateTo('sizes')"
        >
          <i class="nav-icon">📏</i>
          <span>尺寸列表</span>
        </div>
        
        <div 
          class="nav-item" 
          :class="{ active: activeMenu === 'stock' }"
          @click="navigateTo('stock')"
        >
          <i class="nav-icon">📊</i>
          <span>库存管理</span>
        </div>
      </nav>
    </div>
    
    <!-- 主内容区域 -->
    <div class="main-content">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ProductManagement',
  data() {
    return {
      activeMenu: 'products'
    }
  },
  mounted() {
    // 根据当前路由设置活动菜单
    this.setActiveMenuFromRoute();
  },
  watch: {
    '$route'() {
      this.setActiveMenuFromRoute();
    }
  },
  methods: {
    navigateTo(menu) {
      this.activeMenu = menu;
      const targetRoute = `/product-management/${menu}`;
      // 检查当前路由是否已经是目标路由，避免重复导航
      if (this.$route.path !== targetRoute) {
        this.$router.push(targetRoute).catch(err => {
          // 忽略导航重复错误
          if (err.name !== 'NavigationDuplicated') {
            console.error('路由导航错误:', err);
          }
        });
      }
    },
    setActiveMenuFromRoute() {
      const path = this.$route.path;
      if (path.includes('products')) this.activeMenu = 'products';
      else if (path.includes('brands')) this.activeMenu = 'brands';
      else if (path.includes('types')) this.activeMenu = 'types';
      else if (path.includes('colors')) this.activeMenu = 'colors';
      else if (path.includes('sizes')) this.activeMenu = 'sizes';
      else if (path.includes('stock')) this.activeMenu = 'stock';
    }
  }
}
</script>

<style scoped>
.product-management {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
}

.sidebar {
  width: 280px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  padding: 30px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  position: relative;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
}

.logo .icon {
  font-size: 24px;
  margin-right: 12px;
}

.triangle {
  position: absolute;
  top: 50%;
  right: 20px;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid white;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
}

.sidebar-nav {
  flex: 1;
  padding: 20px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 15px 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 0 25px 25px 0;
  margin: 5px 0;
  margin-right: 20px;
  position: relative;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  transform: translateX(5px);
}

.nav-item.active {
  background-color: #ff6b6b;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background-color: white;
  border-radius: 0 2px 2px 0;
}

.nav-icon {
  font-size: 18px;
  margin-right: 12px;
  width: 20px;
  text-align: center;
}

.main-content {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
  background-color: #f8fafc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 70px;
  }
  
  .sidebar-header .logo span,
  .nav-item span {
    display: none;
  }
  
  .triangle {
    display: none;
  }
  
  .nav-item {
    justify-content: center;
    margin-right: 10px;
  }
  
  .nav-icon {
    margin-right: 0;
  }
}
</style>